﻿using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;

namespace RemoteDesktop
{
    /// <summary>
    /// WindowScreen.xaml 的交互逻辑
    /// </summary>
    public partial class WindowScreen : Window
    {
        private System.Windows.Point _pressedPosition;
        private bool _isDragMoved = false;
        private double ScreenWidth = 0;
        private int TFontSize = 38;
        private long OldTime;
        private bool IsShowDate = false;

        public WindowScreen()
        {
            InitializeComponent();
            Init();
        }

        internal void SetTheWords(string currentText)
        {
            ScreenWord.Content = currentText;
            EditBox.Text = currentText;
            SetCenter();
        }

        private void Init()
        {
            if (ScreenWord.Content==null || "".Equals(ScreenWord.Content))
            {
                IsShowDate = true;
            }
            //Width = SystemParameters.PrimaryScreenWidth;
            //Height = SystemParameters.PrimaryScreenHeight;
            //ScreenWord.Width = Width;
            //ScreenWord.Height = Height;
            //Width = ScreenWord.Width;
            ScreenWidth = SystemParameters.PrimaryScreenWidth;
            //DoubleAnimation.From = Width;
            int y = 10;
            this.Top = y; //窗体的位置由Location属性决定
            ScreenWord.FontSize = TFontSize;
            SetCenter();
        }

        private void SetCenter()
        {
            string currentText = (string)ScreenWord.Content;
            if(currentText == null || "".Equals(currentText))
            {
                return;
            }
            Font myFont = new Font("Arial", TFontSize);
            float textWidth = TextWidthCalculator.CalculateTextWidth(currentText, myFont);
            ScreenWord.Width = textWidth;
            Width = textWidth;
            int x = (int)(ScreenWidth - Width) / 2;
            this.Left = x;         //窗体的起始位置为(x,y)
            if(Width > ScreenWidth)
            {
                Width = ScreenWidth;
            }
            EditBox.Width = Width - 20;
            Height = RootPanel.Height;
        }

        private void Window_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _pressedPosition = e.GetPosition(this);
        }

        private void Window_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (Mouse.LeftButton == MouseButtonState.Pressed && _pressedPosition != e.GetPosition(this))
            {
                _isDragMoved = true;
                DragMove(); // 使用DragMove方法移动窗口
            }
        }

        private void Window_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_isDragMoved)
            {
                _isDragMoved = false;
                e.Handled = true; // 标记事件已处理，防止其他控件响应鼠标事件
            }
        }

        private void ScreenWord_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            EditBoxContainer.Visibility = Visibility.Visible;
        }

        private void EditBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            ScreenWord.Content = EditBox.Text;
            SetCenter();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if ("".Equals(EditBox.Text))
            {
                IsShowDate = true;
            }
            else
            {
                IsShowDate = false;
                ScreenWord.Content = EditBox.Text;
            }
            SetCenter();
            EditBoxContainer.Visibility = Visibility.Collapsed;
        }

        private void Window_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            EditBoxContainer.Visibility = Visibility.Visible;
            EditBox.Width = Width - 20;
        }

        internal void ShowEditBox()
        {
            EditBoxContainer.Visibility = Visibility.Visible;
            EditBox.Width = Width - 20;
        }
        bool IsFirst = true;
        internal void UpdateDateTime()
        {
            if (IsShowDate)
            {
                DateTime currentTime = DateTime.Now;
                long currentTimeMillis = currentTime.Ticks / 10000;
                if (currentTimeMillis - OldTime < 1000)
                {
                    return;
                }
                OldTime = currentTimeMillis;
                ScreenWord.Content = currentTime.ToString("yyyy年MM月dd HH:mm:ss");
                if (IsFirst)
                {
                    SetCenter();
                    IsFirst = false;
                }
            }
        }
    }
}
